Skip to content

feat(core): Expose previous entity state on CollectionEvent updates - #5067

Closed
grolmus wants to merge 3 commits into
vendurehq:minorfrom
grolmus:mgrolmus/collection-event-previous-entity
Closed

feat(core): Expose previous entity state on CollectionEvent updates#5067
grolmus wants to merge 3 commits into
vendurehq:minorfrom
grolmus:mgrolmus/collection-event-previous-entity

Conversation

@grolmus

@grolmus grolmus commented Jul 30, 2026

Copy link
Copy Markdown
Collaborator

Summary

Adds a previousEntity to CollectionEvent, populated for updated events, so subscribers can diff a collection against its pre-update state. Builds on and supersedes #4402 by @flobacher (their original commit is preserved).

Root cause / motivation

CollectionEvent only exposed the post-update entity, so a subscriber wanting to react to what changed on a collection update had no access to the previous values.

The original PR (#4402) added previousEntity but loaded it via a bare getEntityOrThrow with no relations, leaving translations (and the other relations) empty — so the previous snapshot was not actually usable for diffing. It also introduced a second collection load alongside the existing active-channel guard.

Change

  • collection-event.ts — new previousEntity?: Collection constructor field, documented with @since 3.8.0. The doc notes its relation set is not identical to entity, so reliable diffing is limited to translations, featuredAsset, assets and scalar columns.
  • collection.service.tsupdate() loads the pre-update collection once, with relations: ['featuredAsset', 'assets', 'channels', 'parent', 'translations']. This single load both enforces the active-channel guard (unchanged semantics — findOneInChannel still filters by channel) and captures the previous snapshot, which is passed to the updated CollectionEvent. The redundant second load is removed. productVariants/children are deliberately excluded (potentially huge).
  • Mutation-safety: TypeORM has no persistent identity map and translatableSaver.update builds a brand-new entity for the save, so previousEntity is a detached pre-update snapshot in all update paths (including filter changes).

Test plan

Automated — extends entity-event-update-state.e2e-spec.ts (the suite that already covers post-update state for Promotion/ShippingMethod/Administrator/PaymentMethod events) with a CollectionEvent case: update a collection's name, assert event.type === 'updated', event.entity has the new name, event.previousEntity has the old name, and that a non-translation relation (channels, parent) is loaded on the snapshot (guards against silently dropping a relation).

Verified against a freshly-built core:

✓ entity-event-update-state.e2e-spec.ts (13 tests)   # incl. the new CollectionEvent case
✓ collection.e2e-spec.ts (78 tests)                  # update() path regression

Without the fix, event.previousEntity is undefined and the new assertions fail.

Closes #4402


View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need. Autofix is disabled.

flobacher and others added 3 commits July 30, 2026 13:25
Load the pre-update Collection with its featuredAsset, assets, channels,
parent and translations so the previousEntity exposed on `updated`
CollectionEvents is usable for diffing — a bare load left translations and
the other relations empty. Consolidates the redundant channel-guard load,
documents the new field with @SInCE 3.8.0, and adds e2e coverage asserting
the previous name is carried through on update.
@vercel

vercel Bot commented Jul 30, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
vendure-storybook Ready Ready Preview Jul 30, 2026 12:09pm

Request Review

@coderabbitai

coderabbitai Bot commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 1d58c5c9-279c-428a-a0d1-17caf18b4aaa

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@michaelbromley

Copy link
Copy Markdown
Member

Hi Martin,

The PR this comes from is rightly labeled T3: systemic. That means it should not be blindly implemented.

This is a perfect case in fact, because we should not be ad-hoc solving this for a single event. We need to step back and think about the general problem of "how do we convey the specific changes to an entity via the event system?"

@vendure-ci-automation-bot vendure-ci-automation-bot Bot locked and limited conversation to collaborators Jul 30, 2026
@grolmus

grolmus commented Jul 30, 2026

Copy link
Copy Markdown
Collaborator Author

@michaelbromley ah u are right. i didnot notice that it is T3

thanks for your check

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants